########################################################################
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 
#


OBDIR := obj
BINDIR := bin
LIBDIR := lib

RM := rm -rf
LIBS :=  -L$(LIBDIR) -L/usr/local/lib -ldl
LIBCPP := -lstdc++ -lpthread
INC :=  -I/usr/local/include/ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)"
DBGDEFS:= -DDEBUG 
REDEFS := -DRELEASE 
CC := gcc
# All of the sources participating in the build are defined here

######################### library
LL_SRCS = cmdLine.cpp Abstract.cpp  

LL_OBJS = $(OBDIR)/cmdLine.o $(OBDIR)/Abstract.o

LL_DEPS = $(OBDIR)/cmdLine.d Abstract/Abstract.d

LL_OUT = libcmdLine.a

######################### Test App
TS_SRCS = cmdLineTest.cpp

TS_OBJS = $(OBDIR)/cmdLineTest.o

TS_DEPS = $(OBDIR)/cmdLineTest.d

TS_OUT = cmdLineTest


# All Target
all: dirs  $(LL_OUT) $(TS_OUT)

release: clean dirs $(LL_OUT) $(TS_OUT)

debug: dirs $(LL_OUT) $(TS_OUT)


# Each subdirectory must supply rules for building sources it contributes
DEFS := $(DBGDEFS)
ifeq ($(MAKECMDGOALS),spi)
	DEFS := $(REDEFS) -DSPIRAM_TEST
else
	ifeq ($(MAKECMDGOALS),release)
		DEFS := $(REDEFS)
	else 
		ifeq ($(MAKECMDGOALS),spi2file)
			DEFS := $(REDEFS)
		endif	
	endif
endif
 
#$(OBDIR)/%.o: %.c
#	@echo 'Building file: $<'
#	@echo 'Invoking: GCC C Compiler'
#	$(CC) $(DEFS) $(INC)  -o"$@" "$<"
#	@echo 'Finished building: $<'
#	@echo ' '

$(OBDIR)/%.o: %.cpp
	@echo 'Building file: $<'
	@echo 'Invoking: G++ C Compiler'
	g++ -Wno-deprecated $(DEFS) $(INC)  -o"$@" "$<"
	@echo 'Finished building: $<'
	@echo ' '


# Tool invocations
$(TS_OUT): $(TS_OBJS) $(USER_OBJS) 
	@echo 'Building target: $@'
	@echo 'Invoking: GCC C Linker'
	$(CC) -L/usr/lib -o $(TS_OUT) $(TS_OBJS) $(LIBS) $(LIBCPP) $(LIBDIR)/libcmdLine.a 
	@echo 'Finished building target: $@'
	mv  $(TS_OUT) $(BINDIR)/ 
	@echo ' '

$(LL_OUT): $(LL_OBJS)
	@echo 'Building target: $@'
	ar rcs $(LL_OUT) $(LL_OBJS) 
	@echo 'Finished building target: $@'
	mv  $(LL_OUT) $(LIBDIR)/  
	@echo ' ' 


dirs:
	mkdir -p -m 755 $(OBDIR)   
	mkdir -p -m 755  $(BINDIR)  
	mkdir -p -m 755  $(LIBDIR)  
	
# Other Targets
clean:
	-$(RM) $(OBDIR)/* $(LIBDIR)/* $(BINDIR)/*
	-@echo ' '


